package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.demo.entity.Person;
import com.demo.entity.User;
import com.demo.mapper.PersonMapper;
import com.demo.mapper.UserMapper;
import com.demo.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Ymj
 * @since 2020-11-23
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    private UserMapper userMapper;
    private PersonMapper personMapper;
    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Autowired
    public void setPersonMapper(PersonMapper personMapper) {
        this.personMapper = personMapper;
    }
    @Override
    public int login(User user) {//0:账户不存在 1:登录成功 2:密码错误
        User userDB = userMapper.selectByUsername(user.getUsername());
        if (userDB==null) return 0;
        if (userDB.getPassword().equals(user.getPassword())) return 1;
        return 2;
    }

    @Override
    public void updateUser() {
        List<User> userList = userMapper.selectList(null);
        userMapper.delete(null);
        List<Person> personList = personMapper.selectList(null);
        User user = new User();
        Long id=System.currentTimeMillis();
        for (int i =0;i<personList.size();i++){
            Boolean isTrue = false;
            for (int j=0;j<userList.size();j++){
                if (userList.get(j).getUsername().equals(String.valueOf(personList.get(i).getId()))){
                    userMapper.insert(userList.get(j));
                    isTrue = true;
                }
            }
            if(isTrue==false){
                user.setId(id);
                user.setUsername(String.valueOf(personList.get(i).getId()));
                userMapper.insert(user);
                id++;
            }
        }

    }

    @Override
    public void updatePassword(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("u_username",user.getUsername());
        userMapper.update(user,wrapper);
    }
}
